package com.framework;

import java.lang.reflect.Field;

class RequiredRule implements ValidationRule {
    private final String field;

    public RequiredRule(String field) {
        this.field = field;
    }

    @Override
    public ValidationResult validate(Object object) {
        try {
            Field f = object.getClass().getDeclaredField(field);
            f.setAccessible(true);
            Object value = f.get(object);

            if (value == null) {
                return ValidationResult.invalid(field, "REQUIRED", "字段不能为空");
            }
            if (value instanceof String && ((String) value).isBlank()) {
                return ValidationResult.invalid(field, "REQUIRED", "字段不能为空");
            }

            return ValidationResult.valid();
        } catch (Exception e) {
            return ValidationResult.invalid(field, "SYSTEM_ERROR", "系统错误: " + e.getMessage());
        }
    }
}